package eu.goldenak.ircbot.module;

import java.util.EnumSet;
import java.util.HashMap;
import java.util.Set;
import java.util.Vector;

import eu.goldenak.ircbot.ConfigManager;
import eu.goldenak.ircbot.game.Events;
import eu.goldenak.ircbot.game.Kill;
import eu.goldenak.ircbot.game.Map;
import eu.goldenak.ircbot.game.Player;
import eu.goldenak.ircbot.BotGroup.DistributeMessage;

public class GameEcho extends ModuleBase implements Events {

	/**
	 * The config loaded from GameEcho.cfg.
	 */
	private HashMap<String, String> config = null;
	
	/**
	 * Messages to echo;
	 */
	private Vector<DistributeMessage> messages;
	
	/**
	 * The channel to echo on.
	 */
	private String channel;
	
	private Boolean customMessages;
	
	/**
	 * Default constructor.
	 */
	public GameEcho() {
		try {
			config = ConfigManager.getModule ("CoD4EchoHandler");
		} catch (Exception ex) {
			System.out.println("Error: " + ex);
			ex.printStackTrace();
			System.exit(1);
		}
		channel = config.get("echochannel");
		customMessages = Boolean.parseBoolean(config.get("custommessages"));
		
	}
		
	@Override
	public void onChatLine(Player p, String message) {
		
		String msg;
		
		if(customMessages){
			String format = config.get("onChatLine");
			format.replace("%p", p.getName());
			format.replace("%t", p.getTeamname());
			format.replace("%cm", message);
			msg = replaceFormat(format);
		} else {
			msg = Color.GREY.toString() + p.getID() + Color.ORANGE.toString() + p.getName() + ": " + Format.RESET + message + "\n";
		}		
		
		messages.add(new DistributeMessage(channel, msg));
	}

	@Override
	public void onGameStart() {
		String msg;
		msg = "Gamhandling of server: " + " started.";
		messages.add(new DistributeMessage(channel, msg));
	}

	@Override
	public void onGameStop() {
		String msg;
		msg = "Gamehandling of server: " + " started.";
		messages.add(new DistributeMessage(channel, msg));
	}

	@Override
	public void onKill(Kill k) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onMapChange(Map pPrevious, Map pNext) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public void onPlayerConnect(Player p) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPlayerDisconnect(Player p) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPlayerJoin(Player p) {
	
	}
	
	/**
	 * Returns the messages which need to be distributed over the bots in an
	 * array.
	 */
	public DistributeMessage[] getDistributeMessages() {
		if (messages.size() == 0) {
			return null;
		}
		
		DistributeMessage[] dm = new DistributeMessage [messages.size ()];
		dm = messages.toArray (dm);
		
		messages.clear();
		
		return dm;
	}
	
	private String replaceFormat(String format) {
		Set <Color> colors = EnumSet.allOf(Color.class);
		
		int i = 0;
		for (Color color : colors) {
			format.replace("@k" + i, color.toString ());
		}
		
		format.replace("@b", Format.BOLD.toString());
		format.replace("@u", Format.ULINE.toString());
		format.replace("@r", Format.RESET.toString());
		
		return null;
	}
	
}